{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 加密"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 介绍2个函数\n",
    "# 得到字符的的ASCII码\n",
    "# print(ord('A'))  # 'A' -> 65\n",
    "# print(ord('H'))  # 'H' -> 72\n",
    "\n",
    "# 得到ASCII码对应的字符\n",
    "# print(chr(65+3)) # 65+3=68 -> 'D'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defgabc\n"
     ]
    }
   ],
   "source": [
    "str1 = input(\"请输入要加密的内容\")\n",
    "key = 3\n",
    "str2 = \"\"\n",
    "for c in str1:\n",
    "    if 'a'<=c<='z':\n",
    "        # 得到后面第3个字符\n",
    "        # str2 = str2 + chr(ord(c)+3)\n",
    "        # 处理xyz的情况\n",
    "        str2 = str2 + chr(ord('a')+(ord(c)-ord('a')+key)%26)\n",
    "    elif 'A'<=c<='Z':\n",
    "        # str2 = str2 + chr(ord(c)+3)\n",
    "        str2 = str2 + chr(ord('A')+(ord(c)-ord('A')+key)%26)\n",
    "    else:\n",
    "        # 不处理\n",
    "        str2 = str2 + c\n",
    "print(str2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65\n",
      "72\n",
      "D\n"
     ]
    }
   ],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 解密（运算反过来)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdxyz\n"
     ]
    }
   ],
   "source": [
    "str3 = \"\"\n",
    "for c in str2:\n",
    "    if 'a'<=c<='z':\n",
    "        # 处理xyz个字符\n",
    "        str3 = str3 + chr(ord('a')+(ord(c)-ord('a')-key+26)%26)\n",
    "    elif 'A'<=c<='Z':\n",
    "        str3 = str3 + chr(ord(c)+3)\n",
    "        str3 = str3 + chr(ord('A')+(ord(c)-ord('A')-key+26)%26)\n",
    "    else:\n",
    "        # 不处理\n",
    "        str3 = str3 + c\n",
    "print(str3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
